/*
 * Transformer - transform xml and csv files
 * Created on 1. August 2007, 11:45
 */
package com.google.cse.emea.universaltransformer;

import com.google.cse.emea.universaltransformer.transformers.TransformerModule;
import com.google.cse.emea.jct.utils.Profile;
import com.google.cse.emea.jct.utils.SimpleFileFilter;
import com.google.cse.emea.universaltransformer.editors.EditorInterface;
import com.google.cse.emea.universaltransformer.editors.EditorFactory;
import com.google.cse.emea.universaltransformer.transformers.TransformerFactory;
import java.awt.CardLayout;
import java.awt.Desktop;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;

import static java.util.logging.Level.*;
import static com.google.cse.emea.universaltransformer.TransformLogger.*;

/**
 * Transform xml or csv files, specially designed for converting adwords editor
 * files and bulkload files. All specific configuration is stored in aproperties
 * file and in the xsl script. It should be easy to adopt the program for other
 * conversion tasks: The following transformations are supported:
 *
 *    from xml to xml, txt, csv (or any other text based format)
 *    from csv to xml, txt ( or any other format)
 *
 * Features:
 * 1. History based: Settings will be stored and loaded on next start automatically.
 * 2. XML output can be schema validated.
 * 3. Files may be compressed (TODO: Specify the entry name, using property now)
 *    For Conversions from CSV a intermediary fxml file will be created, so xsl
 *    transforamtions can be applied to it. The format is the following:
 *
 *  <table>
 *    <row index="1">
 *        <cell name="column name 1" index="0">value11</cell>
 *        <cell name="column name 2" index="1">value12</cell>
 *        ...
 *    </row>
 *    <row index="2">
 *        <cell name="column name 1" index="0">value21</cell>
 *        <cell name="column name 2" index="1">value22</cell>
 *        ...
 *    </row>
 *    ...
 * </table>
 *
 * Be aware, that the first row index is 1, because the title line
 * will not be presented as row.
 *
 *
 * Usage: java -jar Transformer.jar http://url/to/my/config.properties
 *
 * Properties format is the standard ascii properties. It may also be used to
 * localize. Current Settings are stored using Java Preferences. On Windows
 * use regedit to clean or change the settings. On Unix-based systems look for a
 * "~/.comgoogle" properties file.
 *
 *
 * @author  jtrapp@google.com
 */
public class Transformers extends javax.swing.JFrame {

    public static String VERSION = "1.3.2";
    /** Version of the Transformer */
    private Vector<String> inputFilenameHistory; // previously entered input filenames
    private Vector<String> outputFilenameHistory; // previously entered output filenames
    private static boolean openExternallySupported; // Check, if the Operating system supports "open file"
    private TransformerModule transformer;  //Current Transformer (may change)
    private Profile taskListConfiguration; //Settings for all available tasks 
    private Vector<Profile> taskList; // List of tasks, each task is defined in a profle
    private Profile taskConfiguration; // Current task settings

    static {
        if (Desktop.isDesktopSupported()) {
            Desktop desktop = Desktop.getDesktop();
            openExternallySupported = desktop.isSupported(Desktop.Action.OPEN);
        } else {
            openExternallySupported = false;
        }
    }

    /**
     * Creates new form AWEImport
     * @param configurationURL URL to setting file
     * @throws IOException an io error
     */
    public Transformers(String tasksConfigURL, String configurationURL) throws IOException {

        initComponents();
        TransformLogger.setOutputTextArea(logOutputTextArea, statusLabel);

        this.setIconImage(
                new javax.swing.ImageIcon(getClass().getResource("/com/google/cse/emea/jct/images/JCT.icon.gif")).getImage());

        initTaskListUI(tasksConfigURL, configurationURL);
        boolean showLog = taskListConfiguration.getBoolean("log.show");
        showLogCheckBox.setSelected(showLog);
        logScrollPane.setVisible(showLog);

//        addKeyEventsToComboBoxes();
    }

//   
//    private void addKeyEventsToComboBoxes() {
//        final JTextComponent outputEditor = (JTextComponent) outputFilenameComboBox.getEditor().getEditorComponent();
//
//        outputEditor.addKeyListener(new KeyListener() {
//
//            public void keyTyped(KeyEvent e) {
//                String filename = outputEditor.getText();
//                File outputFile = new File(filename);
//                boolean outputFileReadable = outputFile.exists() && outputFile.canRead();
//                viewOutputButton.setEnabled(outputFileReadable && checkOutputEditor());
//                openOutputButton.setEnabled(outputFileReadable && openExternallySupported);
//            }
//
//            public void keyPressed(KeyEvent e) {
//            }
//
//            public void keyReleased(KeyEvent e) {
//            }
//        });
//
//        final JTextComponent inputEditor = (JTextComponent) outputFilenameComboBox.getEditor().getEditorComponent();
//
//        inputEditor.addKeyListener(new KeyListener() {
//
//            public void keyTyped(KeyEvent e) {
//                String filename = inputEditor.getText();
//                File inputFile = new File(filename);
//                boolean inputFileAvailable = inputFile.exists() && inputFile.canRead();
//                viewOutputButton.setEnabled(inputFileAvailable && checkInputEditor());
//                openOutputButton.setEnabled(inputFileAvailable && openExternallySupported);
//                convertButton.setEnabled(inputFileAvailable);
//            }
//
//            public void keyPressed(KeyEvent e) {
//            }
//
//            public void keyReleased(KeyEvent e) {
//            }
//        });
//    }
    /** Take the input filename and reuse it for the output name.
     * Therfore the extension used for the input filename will be exchanged
     * with the extenion from the output file.
     * @param file
     */
    private String getOutputFilenameFromInputFilename(File file) {
        String baseFilename = removeExtension(file.getAbsolutePath(), taskConfiguration.getArray(TransformProfileEnum.INPUT_EXT, ","));

        String[] extensions = taskConfiguration.getArray(TransformProfileEnum.OUTPUT_EXT, ",");
        if (extensions.length >= 0) {
            return baseFilename + extensions[0];
        } else {
            return baseFilename;
        }
    }

    /**
     * Update UI with settings
     */
    private void initTaskListUI(String tasksConfigURL, String configurationURL) throws IOException {
        taskListConfiguration = Profile.get(this.getClass(), tasksConfigURL);

        taskList = TaskList.getTasks(this.getClass(), taskListConfiguration);

        for (TransformerModule t : TransformerFactory.getFactory().getTransformers()) {
            String name = t.getName();
            inputControlsPanel.add(t.getInputPanel(), name);
        }

        taskComboBox.setModel(new DefaultComboBoxModel(taskList));

        if (taskList.size() > 0) {
            int index = TaskList.getDefaultTaskIndex(taskListConfiguration);
            if (configurationURL != null) {
                taskConfiguration = Profile.get(this.getClass(), configurationURL);

                for (int i = 0; i < taskList.size(); i++) {
                    Profile p = taskList.get(i);
                    if (p == taskConfiguration) {
                        index = i;
                    }
                }
            }
            taskComboBox.setSelectedIndex(index);
        }


        setTitle("Transformer: " + taskListConfiguration.get(TaskList.NAME));


    }

    /** 
     * Check, if an editor is configured for the input file.
     * 
     * @return true, if there is an editor
     */
    private boolean checkInputEditor() {
        String type = taskConfiguration.get(TransformProfileEnum.INPUT_TYPE);
        EditorInterface editor = EditorFactory.getEditor(type);
        return (editor != null);

    }

    /** 
     * Check, if an editor is configured for the output file.
     * 
     * @return true, if there is an editor
     */
    private boolean checkOutputEditor() {
        String type = taskConfiguration.get(TransformProfileEnum.OUTPUT_TYPE);
        EditorInterface editor = EditorFactory.getEditor(type);
        return (editor != null);
    }

    /**
     * Initialize the UI for the currently selected task from the task list.
     * @throws java.io.IOException
     */
    private void initTaskUI() throws IOException {

        transformer = TransformerFactory.getTransformer(taskConfiguration.get(TransformProfileEnum.TRANSFORM_MODE));
        fillTable();
        transformer.setProfile(taskConfiguration);

        charsetCombobox.setModel(new DefaultComboBoxModel(Charset.availableCharsets().values().toArray()));
        charsetCombobox.getModel().setSelectedItem(taskConfiguration.get(TransformProfileEnum.OUTPUT_ENCODING));
        charsetCombobox.setEnabled(changeCharsetCheckbox.isSelected());

        inputFilenameHistory = taskConfiguration.loadHistory(TransformProfileEnum.INPUT_TYPE, 10);
        inputFilenameComboBox.setModel(new DefaultComboBoxModel(inputFilenameHistory));
        outputFilenameHistory = taskConfiguration.loadHistory(TransformProfileEnum.OUTPUT_TYPE, 10);
        outputFilenameComboBox.setModel(new DefaultComboBoxModel(outputFilenameHistory));

        String inputFileName = taskConfiguration.get(TransformProfileEnum.INPUT_FILE);
        inputFilenameComboBox.getModel().setSelectedItem(inputFileName);

        String outputFileName = taskConfiguration.get(TransformProfileEnum.OUTPUT_FILE);
        outputFilenameComboBox.getModel().setSelectedItem(outputFileName);

        //do localizaton
        inputFileLabel.setText(taskConfiguration.get(TransformProfileEnum.LABEL_INPUT_FILE));
        openInputButton.setText(taskConfiguration.get(TransformProfileEnum.LABEL_INPUT_OPEN));
        viewInputButton.setText(taskConfiguration.get(TransformProfileEnum.LABEL_INPUT_VIEW));

        outputFileLabel.setText(taskConfiguration.get(TransformProfileEnum.LABEL_OUTPUT_FILE));
        openOutputButton.setText(taskConfiguration.get(TransformProfileEnum.LABEL_OUTPUT_OPEN));
        viewOutputButton.setText(taskConfiguration.get(TransformProfileEnum.LABEL_OUTPUT_VIEW));

        convertButton.setText(taskConfiguration.get(TransformProfileEnum.LABEL_ACTION));
        CardLayout layout = (CardLayout) inputControlsPanel.getLayout();
        if (transformer != null) {
            String name = transformer.getName();
            layout.show(inputControlsPanel, name);
        } else {
            layout.show(inputControlsPanel, "empty");
        }

        TaskList.setProfile(taskListConfiguration, taskConfiguration);
    }

    /** Fill table from properties file
     * @param table to fill
     */
    private void fillTable() {
        ParameterTableModel ptm = new ParameterTableModel(taskConfiguration);
        parameterTable.setModel(ptm);
        int numcolumns = ptm.getRowCount();

        parameterTable.setVisible(numcolumns > 0);
    }

    private String removeExtension(String filename, String[] extensions) {
        if (extensions != null) {
            for (int i = 0; i < extensions.length; i++) {

                if (filename.endsWith(extensions[i])) {
                    return filename.substring(0, filename.length() - extensions[i].length());
                }
            }
        }
        return filename;
    }

    /**
     * Set the input file and update the UI.
     * @param filename Filename
     */
    private boolean setInputFile(String filename) {
        return setInputFile(new File(filename));
    }

    /**
     * Set the input file and update the UI.
     * @param file Input file
     */
    private boolean setInputFile(File file) {
        boolean retValue = false;
        Object oldInputFileObject = inputFilenameComboBox.getModel().getSelectedItem();
        inputFilenameComboBox.getModel().setSelectedItem(file.getAbsoluteFile());
        //This is where a real application would open the file.
        if (file.exists()) {

            status("Opened: " + file.getName() + " Compressed size:" + file.length() + ".");
            retValue = true;
            taskListConfiguration.updateHistory(TransformProfileEnum.INPUT_TYPE, inputFilenameHistory, file.getAbsolutePath());
            taskConfiguration.put(TransformProfileEnum.INPUT_FILE, file.getAbsolutePath());

            //Doing some magic to adopt name for the output file:

            Object oldOutputFileObject = outputFilenameComboBox.getModel().getSelectedItem();
            if ((oldOutputFileObject == null) || (oldOutputFileObject.toString().length() == 0)) { // No previous name set
                setOutputFile(getOutputFilenameFromInputFilename(file));
                return retValue;
            } else if (taskListConfiguration.getBoolean(TaskList.OUTPUT_FILE_FOLLOW_INPUT, true) && oldInputFileObject != null) {

                if (new File(oldInputFileObject.toString()).isDirectory()) {
                    setOutputFile(getOutputFilenameFromInputFilename(file));
                    return retValue;
                }

                String oldInputFilename = removeExtension(
                        oldInputFileObject.toString(),
                        taskConfiguration.getArray(TransformProfileEnum.INPUT_EXT, ","));

                String oldOutputFileName = removeExtension(
                        oldOutputFileObject.toString(),
                        taskConfiguration.getArray(TransformProfileEnum.OUTPUT_EXT, ","));
                if (oldInputFilename.equals(oldOutputFileName)) {
                    setOutputFile(getOutputFilenameFromInputFilename(file));
                    return retValue;
                }
            }
        }
        updateConversionUI();
        return retValue;
    }

    private void setInputFilename() {
        Object selectedObject = inputFilenameComboBox.getSelectedItem();
        String selectedFile = selectedObject == null ? System.getProperty("user.dir") : selectedObject.toString();
        if (setInputFile(selectedFile)) {

//  Don't parse on opening          if (transformer != null) {
//
//                if (selectedFile.length() > 0) {
//                    File file = new File(selectedFile);
//                    if (file.exists()) {
//                        transformer.parse(selectedFile);
//                    }
//                }
//            }
        }
    }

    /**
     * Set the output file and update the UI.
     */
    private void setOutputFile(String filename) {
        if (filename == null || filename.length() == 0) {
            Object inputFilenameObject = inputFilenameComboBox.getSelectedItem();
            if ((inputFilenameObject != null) && (inputFilenameObject.toString().length() > 0)) {
                filename = getOutputFilenameFromInputFilename(new File(inputFilenameObject.toString()));
            }
        }
        File file = new File(filename);
        setOutputFile(file);
    }

    /**
     * Set the output file and update the UI.
     */
    private void setOutputFile(File file) {
        outputFilenameComboBox.getModel().setSelectedItem(file.getAbsoluteFile());
        //This is where a real application would open the file.
        if (file.exists()) {
            if (file.isDirectory()) {
                status("Please Specify output file.");
            } else {
                taskListConfiguration.updateHistory(TransformProfileEnum.OUTPUT_TYPE, outputFilenameHistory, file.getAbsolutePath());
                taskConfiguration.put(TransformProfileEnum.OUTPUT_FILE, file.getAbsolutePath());
                status("Output file " + file.getName() + " already exists. File will be overwritten.");
            }
        }

        updateConversionUI();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        inputFileLabel = new javax.swing.JLabel();
        inputBrowseButton = new javax.swing.JButton();
        convertButton = new javax.swing.JButton();
        inputFilenameComboBox = new javax.swing.JComboBox();
        viewInputButton = new javax.swing.JButton();
        outputBrowseButton = new javax.swing.JButton();
        outputFilenameComboBox = new javax.swing.JComboBox();
        outputFileLabel = new javax.swing.JLabel();
        logScrollPane = new javax.swing.JScrollPane();
        logOutputTextArea = new javax.swing.JTextArea();
        viewOutputButton = new javax.swing.JButton();
        openOutputButton = new javax.swing.JButton();
        openInputButton = new javax.swing.JButton();
        inputControlsPanel = new javax.swing.JPanel();
        emptyInputPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        parameterLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        parameterTable = new javax.swing.JTable();
        taskComboBox = new javax.swing.JComboBox();
        taskLabel = new javax.swing.JLabel();
        outputControlsPanel = new javax.swing.JPanel();
        emptyOutputPanel = new javax.swing.JPanel();
        charsetCombobox = new javax.swing.JComboBox();
        changeCharsetCheckbox = new javax.swing.JCheckBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        statusLabel = new javax.swing.JLabel();
        showLogCheckBox = new javax.swing.JCheckBox();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        saveConfigurationMenuItem = new javax.swing.JMenuItem();
        loadMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Converter");

        inputFileLabel.setText("Input File:");

        inputBrowseButton.setText("Browse ...");
        inputBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseInputFile(evt);
            }
        });

        convertButton.setText("Transform");
        convertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                convertButtonActionPerformed(evt);
            }
        });

        inputFilenameComboBox.setEditable(true);
        inputFilenameComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                inputFileChanged(evt);
            }
        });
        inputFilenameComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileNameEntered(evt);
            }
        });
        inputFilenameComboBox.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                inputFilenameComboBoxInputMethodTextChanged(evt);
            }
        });
        inputFilenameComboBox.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                inputFilenameComboBoxKeyTyped(evt);
            }
        });

        viewInputButton.setText("View Input File...");
        viewInputButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewInputButtonActionPerformed(evt);
            }
        });

        outputBrowseButton.setText("Browse ...");
        outputBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseOutputFile(evt);
            }
        });

        outputFilenameComboBox.setEditable(true);
        outputFilenameComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        outputFilenameComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                outputFilenameComboBoxItemStateChanged(evt);
            }
        });
        outputFilenameComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                outputFilenameComboBoxActionPerformed(evt);
            }
        });
        outputFilenameComboBox.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                outputFilenameComboBoxInputMethodTextChanged(evt);
            }
        });
        outputFilenameComboBox.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                outputFilenameComboBoxKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                outputFilenameComboBoxKeyTyped(evt);
            }
        });

        outputFileLabel.setText("Output File:");

        logOutputTextArea.setColumns(20);
        logOutputTextArea.setRows(5);
        logScrollPane.setViewportView(logOutputTextArea);

        viewOutputButton.setText("View Output File ...");
        viewOutputButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewOutputButtonActionPerformed(evt);
            }
        });

        openOutputButton.setText("Open Output File ...");
        openOutputButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openOutputButtonActionPerformed(evt);
            }
        });

        openInputButton.setText("Open Input File ...");
        openInputButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openInputButtonActionPerformed(evt);
            }
        });

        inputControlsPanel.setLayout(new java.awt.CardLayout());

        emptyInputPanel.setName("empty"); // NOI18N

        jLabel1.setText("-");

        javax.swing.GroupLayout emptyInputPanelLayout = new javax.swing.GroupLayout(emptyInputPanel);
        emptyInputPanel.setLayout(emptyInputPanelLayout);
        emptyInputPanelLayout.setHorizontalGroup(
            emptyInputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(emptyInputPanelLayout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(473, Short.MAX_VALUE))
        );
        emptyInputPanelLayout.setVerticalGroup(
            emptyInputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(emptyInputPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );

        inputControlsPanel.add(emptyInputPanel, "empty");

        parameterLabel.setText("Parameter:");

        jScrollPane2.setName("parameterTable"); // NOI18N

        parameterTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Parameter", "Value"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(parameterTable);

        taskComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        taskComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taskComboBoxActionPerformed(evt);
            }
        });

        taskLabel.setText("Task:");

        outputControlsPanel.setLayout(new java.awt.CardLayout());

        emptyOutputPanel.setName("empty"); // NOI18N

        charsetCombobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        charsetCombobox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                charsetComboboxActionPerformed(evt);
            }
        });

        changeCharsetCheckbox.setText("Choose Character Set:");
        changeCharsetCheckbox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        changeCharsetCheckbox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        changeCharsetCheckbox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                changeCharsetCheckboxItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout emptyOutputPanelLayout = new javax.swing.GroupLayout(emptyOutputPanel);
        emptyOutputPanel.setLayout(emptyOutputPanelLayout);
        emptyOutputPanelLayout.setHorizontalGroup(
            emptyOutputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(emptyOutputPanelLayout.createSequentialGroup()
                .addComponent(changeCharsetCheckbox, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(charsetCombobox, 0, 382, Short.MAX_VALUE))
        );
        emptyOutputPanelLayout.setVerticalGroup(
            emptyOutputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(emptyOutputPanelLayout.createSequentialGroup()
                .addContainerGap(38, Short.MAX_VALUE)
                .addGroup(emptyOutputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(changeCharsetCheckbox)
                    .addComponent(charsetCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        outputControlsPanel.add(emptyOutputPanel, "card2");

        statusLabel.setText("2007 JTrapp@Google.com");
        jScrollPane3.setViewportView(statusLabel);

        showLogCheckBox.setSelected(true);
        showLogCheckBox.setText("Show log");
        showLogCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showLogCheckBoxActionPerformed(evt);
            }
        });

        menuBar.setEnabled(false);

        fileMenu.setText("File");
        fileMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuActionPerformed(evt);
            }
        });

        saveConfigurationMenuItem.setText("Save Configuration ...");
        saveConfigurationMenuItem.setEnabled(false);
        saveConfigurationMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveConfigurationMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(saveConfigurationMenuItem);

        loadMenuItem.setText("Load Configuration ...");
        loadMenuItem.setEnabled(false);
        loadMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(loadMenuItem);

        menuBar.add(fileMenu);

        editMenu.setText("Edit");

        jMenuItem1.setText("Edit Settings ...");
        jMenuItem1.setEnabled(false);
        editMenu.add(jMenuItem1);

        menuBar.add(editMenu);

        helpMenu.setText("Help");

        aboutMenuItem.setText("About");
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(logScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 729, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(parameterLabel)
                            .addComponent(inputFileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(taskLabel)
                            .addComponent(showLogCheckBox)
                            .addComponent(outputFileLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(inputControlsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
                                    .addComponent(inputFilenameComboBox, javax.swing.GroupLayout.Alignment.LEADING, 0, 519, Short.MAX_VALUE)
                                    .addComponent(outputFilenameComboBox, 0, 519, Short.MAX_VALUE)
                                    .addComponent(taskComboBox, javax.swing.GroupLayout.Alignment.LEADING, 0, 519, Short.MAX_VALUE)
                                    .addComponent(outputControlsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE))
                                .addGap(6, 6, 6)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(convertButton, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                                    .addComponent(openOutputButton, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(openInputButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                                    .addComponent(viewInputButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                                    .addComponent(inputBrowseButton, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                                    .addComponent(outputBrowseButton, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                                    .addComponent(viewOutputButton, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(taskComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskLabel)
                    .addComponent(convertButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputFilenameComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(inputBrowseButton)
                    .addComponent(inputFileLabel))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(viewInputButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(openInputButton)
                        .addGap(17, 17, 17))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(inputControlsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputFilenameComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(outputBrowseButton)
                    .addComponent(outputFileLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(viewOutputButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(openOutputButton))
                    .addComponent(outputControlsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(parameterLabel)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane3, 0, 0, Short.MAX_VALUE)
                    .addComponent(showLogCheckBox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /** on delimiter change action*/
    /** Store the configuration file.*/
    private void saveConfigurationMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveConfigurationMenuItemActionPerformed
        int returnVal = fileChooser.showSaveDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {

            try {
                taskConfiguration.save(fileChooser.getSelectedFile());
            } catch (IOException ioe) {
                log(SEVERE, "Failed to write configurration: " + ioe.getMessage());
            }
        } else {
            status("Open command cancelled by user.");
        }
}//GEN-LAST:event_saveConfigurationMenuItemActionPerformed


private void openInputButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openInputButtonActionPerformed
    File inputFile = new File(inputFilenameComboBox.getSelectedItem().toString());
    open(inputFile);
}//GEN-LAST:event_openInputButtonActionPerformed

private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
    showLogCheckBox.setSelected(true);
    logScrollPane.setVisible(true);
    validate();
    status("");
    status("Multipurpose converion tool for XML- and CSV-Files");
    status("Tool Version: " + VERSION);
    status("(c) 2007 JTrapp@Google.com");
    status(" ");
    status("Profile" + taskListConfiguration.get(TaskList.NAME));
    status("Profile-Version" + taskListConfiguration.get(TaskList.VERSION));
    status("Description" + taskListConfiguration.get(TaskList.DESCRIPTION));
    status("Profile Author" + taskListConfiguration.get(TaskList.AUTHOR));
    status("Current Task Confiduration:");
    status("\tProfile" + taskConfiguration.get(TransformProfileEnum.NAME));
    status("\tProfile-Version" + taskConfiguration.get(TransformProfileEnum.VERSION));
    status("\tDescription" + taskConfiguration.get(TransformProfileEnum.DESCRIPTION));
    status("\tProfile Author" + taskConfiguration.get(TransformProfileEnum.AUTHOR));
}//GEN-LAST:event_aboutMenuItemActionPerformed

private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileMenuActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_fileMenuActionPerformed

private void changeCharsetCheckboxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_changeCharsetCheckboxItemStateChanged
    charsetCombobox.setEnabled(changeCharsetCheckbox.isSelected());
                if (changeCharsetCheckbox.isSelected()) {
                Charset charset = Charset.forName(charsetCombobox.getSelectedItem().toString());
                taskConfiguration.get(TransformProfileEnum.OUTPUT_ENCODING, charset.displayName());
                status("Converting using " + charset.displayName() + " encoding.");
            } else {
                status("Converting using the windows/excel default encoding");
            }
}//GEN-LAST:event_changeCharsetCheckboxItemStateChanged

private void openOutputButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openOutputButtonActionPerformed
    String filename = outputFilenameComboBox.getSelectedItem().toString();
    setOutputFile(filename);
    open(new File(filename));
}//GEN-LAST:event_openOutputButtonActionPerformed

/** Open file using the system action.*/
    private void open(File file) {
        // Before more Desktop API is used, first check
        // whether the API is supported by this particular
        // virtual machine (VM) on this particular host.
        try {
            if (openExternallySupported) {
                Desktop desktop = Desktop.getDesktop();
                desktop.open(file);
            }
        } catch (IOException ioe) {
            log(WARNING, "Can't open " + file.getName() + " ." + ioe.getMessage());
        }
    }

    private void viewOutputButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
            final String filename= outputFilenameComboBox.getSelectedItem().toString();
            setOutputFile(filename);
            final String type = taskConfiguration.get(TransformProfileEnum.OUTPUT_TYPE);
            final EditorInterface editor = EditorFactory.getEditor(type);
            if (editor!=null){
                new Thread(new Runnable(){
                    public void run(){
                        try {
                             log(Level.INFO, "Start viewing "+filename);
                             editor.viewFile(taskConfiguration, "output.", type);
                             log(Level.INFO, "Viewing "+filename);
                        } catch (IOException ex) {
            log(Level.SEVERE, "Can't view input file: "+inputFilenameComboBox.getSelectedItem(), ex);
                        }
                    }

                }).start();
            }
    }                                                                                            

private void outputFilenameComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_outputFilenameComboBoxActionPerformed
    Object outputFileObject = outputFilenameComboBox.getSelectedItem();
	if (outputFileObject!=null){
		setOutputFile(outputFileObject.toString());
        }	
}//GEN-LAST:event_outputFilenameComboBoxActionPerformed

private void outputFilenameComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {                                                        
    Object outputFileObject = outputFilenameComboBox.getSelectedItem();
	if (outputFileObject!=null){
		setOutputFile(outputFileObject.toString());
		}
}                                                                                                              

private void browseOutputFile(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseOutputFile
    fileChooser.setFileFilter(new SimpleFileFilter(taskConfiguration.get(TransformProfileEnum.OUTPUT_EXT), 
          taskConfiguration.get(TransformProfileEnum.OUTPUT_DESCRIPTION)));
    Object outputFileObject = outputFilenameComboBox.getSelectedItem();
    String outputFilename = taskConfiguration.get(TransformProfileEnum.OUTPUT_FILE);
    if ((outputFileObject==null)||(outputFileObject.toString().length()>0)){
        outputFilename = outputFileObject.toString();
    }
    if ((outputFilename!=null)&&(outputFilename.length()>0)){
        File dir = new File(outputFilename);
        if (dir.isFile()){
            dir=dir.getParentFile();
        }
        fileChooser.setCurrentDirectory(dir);
    } 
    
    int returnVal = fileChooser.showOpenDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        setOutputFile(fileChooser.getSelectedFile());
    } else {
        status("Open command cancelled by user.");
    }
}//GEN-LAST:event_browseOutputFile

    private void viewInputButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
            final String filename = inputFilenameComboBox.getSelectedItem().toString();
            setInputFile(filename);
            final String type = taskConfiguration.get(TransformProfileEnum.INPUT_TYPE);
            final EditorInterface editor = EditorFactory.getEditor(type);
            if (editor!=null){
                new Thread(new Runnable(){
                    public void run(){
                        try {
                             log(Level.INFO, "Start viewing "+filename);
                             editor.viewFile(taskConfiguration, "input.", type);
                             log(Level.INFO, "Viewing "+filename);
                        } catch (IOException ex) {
            log(Level.SEVERE, "Can't view input file: "+inputFilenameComboBox.getSelectedItem(), ex);
                        }
                    }

                }).start();
            }
 
}                                                                                          

private void inputFileChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_inputFileChanged
    setInputFilename();//GEN-LAST:event_inputFileChanged
}                                 

private void fileNameEntered(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileNameEntered
    
    setInputFilename();
}//GEN-LAST:event_fileNameEntered

private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertButtonActionPerformed
    setOutputFile(outputFilenameComboBox.getSelectedItem().toString());
    transformer.parse(inputFilenameComboBox.getModel().getSelectedItem().toString());
    if (transformer.transform(
            outputFilenameComboBox.getModel().getSelectedItem().toString(),
            (ParameterTableModel)parameterTable.getModel())) {
        //Update output action buttons
 //       setOutputFile(outputFilenameComboBox.getSelectedItem().toString());
            updateConversionUI();
    }
}//GEN-LAST:event_convertButtonActionPerformed
    
    
//
//    private boolean convertRegex() {
//       try {
//
//            Charset charset = getCharset(KEY_OUTPUT_ENCODING.getProperty());
//            if (changeCharsetCheckbox.isSelected()) {
//                charset = Charset.forName(charsetCombobox.getSelectedItem().toString());
//                KEY_OUTPUT_ENCODING.setProperty(charset.displayName());
//                status("Converting using " + charset.displayName() + " encoding.");
//            } else {
//                status("Converting using the windows/excel default encoding");
//            }
//            OutputStream stream = null;
//            String filename = outputFilenameComboBox.getSelectedItem().toString();
//            String zipEntry = KEY_OUTPUT_ZIP_ENTRY.getProperty();
//            if (zipEntry != null && zipEntry.length() > 0) {
//                status("Writing compressed file ");
//                stream = new ZipOutputStream(new FileOutputStream(filename));
//                ((ZipOutputStream) stream).putNextEntry(new ZipEntry(KEY_OUTPUT_ZIP_ENTRY.getProperty()));
//            } else {
//                stream = new FileOutputStream(filename);
//            }
//
//            String patternString = regexPatternTextField.getText();
//            String replaceString = regexReplaceTextField.getText();
//            Pattern pattern = Pattern.compile(patternString);
//            String line;
//            while((line= st))
//            
//            
//            
//            if (stream instanceof ZipOutputStream) {
//                ((ZipOutputStream) stream).closeEntry();
//            }
//            stream.close();
//            status("Finished");
//            return true;
//        } catch (Exception e) {
//            status("Failed!" + e.getMessage());
//            log(SEVERE, e);
//        }
//        return false;
//    }  


 

private void browseInputFile(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseInputFile

    fileChooser.setFileFilter(new SimpleFileFilter(taskConfiguration.get(TransformProfileEnum.INPUT_EXT), taskConfiguration.get(TransformProfileEnum.INPUT_DESCRIPTION)));
    String inputFilename = taskConfiguration.get(TransformProfileEnum.INPUT_FILE);
    
    Object inputFileObject = inputFilenameComboBox.getSelectedItem();
    if ((inputFileObject!=null)&&(inputFileObject.toString().length()>0)){
        inputFilename = inputFileObject.toString();
    }
    
    if ((inputFilename!=null)&&(inputFilename.length()>0)){
        File dir = new File(inputFilename);
        if (dir.isFile()){
            dir=dir.getParentFile();
        }
        fileChooser.setCurrentDirectory(dir);
    }        
    int returnVal = fileChooser.showOpenDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {

        setInputFile(fileChooser.getSelectedFile());
    } else {
        status("Open command cancelled by user.");
    }
}//GEN-LAST:event_browseInputFile

            private void inputFilenameComboBoxKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_inputFilenameComboBoxKeyTyped
                updateConversionUI();
            }//GEN-LAST:event_inputFilenameComboBoxKeyTyped

            private void outputFilenameComboBoxKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_outputFilenameComboBoxKeyTyped
                updateConversionUI();
            }//GEN-LAST:event_outputFilenameComboBoxKeyTyped

            private void outputFilenameComboBoxKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_outputFilenameComboBoxKeyReleased
                updateConversionUI();
            }//GEN-LAST:event_outputFilenameComboBoxKeyReleased
			
			
private void loadMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadMenuItemActionPerformed
        int returnVal = fileChooser.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {

            try {
                initTaskListUI(fileChooser.getSelectedFile().getAbsolutePath(),null);
            } catch (IOException ioe) {
                log(SEVERE, "Failed to load configurration: " + ioe.getMessage());
            }
        } else {
            status("Open command cancelled by user.");
        } 
}//GEN-LAST:event_loadMenuItemActionPerformed
            private void outputFilenameComboBoxInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_outputFilenameComboBoxInputMethodTextChanged
                updateConversionUI();
            }//GEN-LAST:event_outputFilenameComboBoxInputMethodTextChanged

            private void inputFilenameComboBoxInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_inputFilenameComboBoxInputMethodTextChanged
                updateConversionUI();
            }//GEN-LAST:event_inputFilenameComboBoxInputMethodTextChanged
			
			
private void charsetComboboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_charsetComboboxActionPerformed
            if (changeCharsetCheckbox.isSelected()) {
                Charset charset = Charset.forName(charsetCombobox.getSelectedItem().toString());
                taskConfiguration.get(TransformProfileEnum.OUTPUT_ENCODING, charset.displayName());
                status("Converting using " + charset.displayName() + " encoding.");
            } else {
                status("Converting using the windows/excel default encoding");
            }
}//GEN-LAST:event_charsetComboboxActionPerformed

private void taskComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taskComboBoxActionPerformed
        try {
            Object item = taskComboBox.getModel().getSelectedItem();
            if (item instanceof Profile) {
                taskConfiguration = (Profile) item;

                initTaskUI();
            } else {
                if (item!=null&&(item.toString().length()!=0)){
                    log(SEVERE, "Unespected selection <"+item+">");
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Transformers.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_taskComboBoxActionPerformed

            private void showLogCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showLogCheckBoxActionPerformed
                boolean showLog= showLogCheckBox.isSelected();
                logScrollPane.setVisible(showLog);
           
                
                taskListConfiguration.put("log.show", showLog);
                validate();
		}//GEN-LAST:event_showLogCheckBoxActionPerformed
                                        

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JCheckBox changeCharsetCheckbox;
    private javax.swing.JComboBox charsetCombobox;
    private javax.swing.JButton convertButton;
    private javax.swing.JMenu editMenu;
    private javax.swing.JPanel emptyInputPanel;
    private javax.swing.JPanel emptyOutputPanel;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JButton inputBrowseButton;
    private javax.swing.JPanel inputControlsPanel;
    private javax.swing.JLabel inputFileLabel;
    private javax.swing.JComboBox inputFilenameComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JMenuItem loadMenuItem;
    private javax.swing.JTextArea logOutputTextArea;
    private javax.swing.JScrollPane logScrollPane;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JButton openInputButton;
    private javax.swing.JButton openOutputButton;
    private javax.swing.JButton outputBrowseButton;
    private javax.swing.JPanel outputControlsPanel;
    private javax.swing.JLabel outputFileLabel;
    private javax.swing.JComboBox outputFilenameComboBox;
    private javax.swing.JLabel parameterLabel;
    private javax.swing.JTable parameterTable;
    private javax.swing.JMenuItem saveConfigurationMenuItem;
    private javax.swing.JCheckBox showLogCheckBox;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JComboBox taskComboBox;
    private javax.swing.JLabel taskLabel;
    private javax.swing.JButton viewInputButton;
    private javax.swing.JButton viewOutputButton;
    // End of variables declaration//GEN-END:variables

 
    /** Check settings and enable the appropriate ui elements.
     * For example, if the input file is not valid, the action button will be
     * disabled.
     */
    private void updateConversionUI() {
        convertButton.setEnabled(false);
        if (inputFilenameComboBox.getSelectedItem() == null || outputFilenameComboBox.getSelectedItem() == null) {
            return; // in initialization
        }
        
        File inputFile = new File(inputFilenameComboBox.getSelectedItem().toString());
        File outputFile = new File(outputFilenameComboBox.getSelectedItem().toString());
        boolean inputFileAvailable = true;
        boolean outputFileReadable = true;
        boolean convertEnabled=false;
        if (inputFile == null || inputFile.getName().length() == 0) {
            status("Please enter input file");
            inputFileAvailable = false;
        } else if (!inputFile.exists()) {
            status("Input file does not exist");
            inputFileAvailable = false;
        } else if (inputFile.isDirectory()) {
            status("Please select input file");
            inputFileAvailable = false;
        } else if (outputFile == null || outputFile.getName().length() == 0) {
            status("Please enter output file.");
            outputFileReadable=false;
        } else if (outputFile.isDirectory()) {
            status("Please select output file");
            outputFileReadable=false;
//        } else if (!outputFile.canWrite()) {
//            status("No write permission for output file. Please specify output file");
       } else {
            convertEnabled=true;
        }
        outputFileReadable= outputFileReadable && outputFile.exists()&&outputFile.canRead();
        viewInputButton.setEnabled(inputFileAvailable&&checkInputEditor());
        openInputButton.setEnabled(inputFileAvailable);
        convertButton.setEnabled(convertEnabled);
        viewOutputButton.setEnabled(outputFileReadable&&checkOutputEditor());
        openOutputButton.setEnabled(outputFileReadable && openExternallySupported);
        
    }

    /**
     * Start the Transformer
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final String taskListURL = (args.length > 0) ? args[0] : "cpath:///com/google/cse/emea/universaltransformer/tasks.properties";
        final String TransformProfileEnumURL = (args.length > 1) ? args[1] : null;
   
        com.google.cse.emea.jct.utils.protocols.HandlerFactory.registerProtocol();
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new Transformers(taskListURL, TransformProfileEnumURL).setVisible(true);
                } catch (IOException ioe) {
                    log(SEVERE, "Error reading the configuration. " + ioe.getMessage(), ioe);
                    System.exit(-1);
                }
            }
        });
    }

}
